<?php
	include("includes/init.inc.php");
	require_once("includes/api.tags.php");
	require_once("includes/api.files.php");

	mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWD) or die(mysql_error());
	mysql_select_db(DB_NAME) or die(mysql_error());
		
	$idFileUploaded = 0;
	$err = "";
	$ok = "";
	
	//Upload de fichiers
	if(isset($_POST['up'])){
		if(is_uploaded_file($_FILES['up_file']['tmp_name']) == 1){
			//Chercher le tag All de l'utilisateur
			$idTag = 0;
			$sql = "select id from label where name='All' and user_id=".$_SESSION['user_id'];
			$req = mysql_query($sql) or die($sql.'<br>'.mysql_error());
			if(mysql_num_rows($req) == 1){
				$enr = mysql_fetch_assoc($req);
				$idTag = $enr['id'];
			}
			
			if($idTag > 0){
				$pos = stripos($_FILES['up_file']['name'],".");	//Trouver le dernier . pour l'extension
				if($_POST['filename'] == "" || $_POST['filename'] == "Name for the file..."){
					$name = substr($_FILES['up_file']['name'],0,$pos);
				}
				else{
					$name = $_POST['filename'];
				}
				$ext = substr($_FILES['up_file']['name'],$pos+1);
				$file = uniqid($_SESSION['user_id'].'_',true);
				
				if(move_uploaded_file($_FILES['up_file']['tmp_name'],'files/'.$file.'.'.$ext)){
					$sql = "insert into file(name,file,ext) values('$name','$file','$ext')";
					$req = mysql_query($sql) or die($sql.'<br>'.mysql_error());
					$idFileUploaded = mysql_insert_id();
					
					$sql = "insert into file_label(file_id,label_id) values($idFileUploaded,$idTag)";
					$req = mysql_query($sql) or die($sql.'<br>'.mysql_error());
				}else{
					$err = "Fail to upload file, please try again later.";
				}
			}
		}
		else{
			$err = "Fail to upload file, please select a file to upload.";
		}
	}
	
	//Set des tags
	if(isset($_POST['file_id']) && $_POST['file_id'] != 0){
		//Saver les nouveau tags
		if(isset($_POST['tags'])){
			foreach($_POST['tags'] as $value){
				$sql = "insert into file_label(file_id, label_id) values(".$_POST['file_id'].",$value)";
				mysql_query($sql) or die($sql.'<br>'.mysql_error());
			}
		}
		$ok = "File saved, you can now upload a new file :";
	}
	
	$title = "Upload a file";
	include("includes/header.inc.php");
?>			
				<td id="contenu" style="height:300px; text-align:center;" align="center">
					<!-- Contenu commence ici -->
					<table border="0" cellpadding="0" cellspacing="0" <?php echo(($idFileUploaded != 0) ? '':'style="width:300px; margin:auto;"')?>>
                    	<?php echo (($err != "") ? '<tr><td style="text-align:center; color:red;">'.$err.'</td></tr>':'')?>
						<?php echo (($ok != "") ? '<tr><td style="text-align:center; color:green;">'.$ok.'</td></tr>':'')?>
						<?php if($idFileUploaded != 0){ ?>
                        <tr>
							<td width="40%" class="title">Tags</td>
							<td width="60%" class="title">Uploaded files</td>
						</tr>
						<tr>
							<td><form name="tags" method="post" action=""><input type="hidden" name="file_id" value="<?php echo $idFileUploaded?>" /><?php
                                $tabTag = get_all_tags_for_current_user(false);
								foreach($tabTag as $row){
									echo '<input type="checkbox" name="tags[]" value="'.$row["id"].'" />'.$row["name"].'<br />';
								}
								echo '<br /><br /><input type="submit" value="Save" />';
							?></form></td>
							<td><?php
								$info = get_file_info($idFileUploaded);
								echo '<img src="'.$info['icon'].'" alt="'.$info['name'].'"><br>'.$info['name'];
							?></td>
						</tr>
                        <?php }else{ ?>
						<tr>
							<td style="text-align:center;">
								<form name="upload" method="post" action="" enctype="multipart/form-data">
									<input type="text" name="filename" value="Name for the file..." style="width:226px;" onFocus="if(this.value == 'Name for the file...')this.value='';" /><br />
									<input type="file" name="up_file" value="" /><br />
									<input type="submit" value="Upload" style="width:84px;" /><input type="hidden" name="up" value="1" />
								</form>
							</td>
						</tr>
                        <?php } ?>
					</table>
					<!-- Contenu fini ici -->
				</td>
<?php
	include("includes/footer.inc.php");
?>